import React from 'react';
import axios from 'axios';
import Qs from 'qs';
import { getCsrfToken } from './../utils/common';
import { notification } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { createHashHistory } from 'history';
import baseURL from './../config/baseurl';

export default class Axios {
	static get(options){
		return new Promise((resolve,reject)=>{
			axios({
				baseURL,
				url: options.url,
				method: "get",
				params: {
					...options.params,
					CSRF: getCsrfToken()
				},
				timeout:5000,
				withCredentials: true,
				crossDomain: true
			}).then((response)=>{
				if (response.status === 200){
					let res = response.data;
					if (res.code === 0){
						resolve(res);
					}else if(res.code === 999 || res.code === 2001){
						const history = createHashHistory();
						history.push('/login');
					}else{
						let index = res.msg.indexOf(" call");
						let message = res.msg;
						if(index>0){
							message = message.slice(0, index);
						}
						notification.open({
							message: '提示',
							description: message,
							icon: <ExclamationCircleOutlined style={{ color: '#108ee9' }} />
						});
						reject(res);
					}
				}else{
					reject(response.data);
				}
			})
		})
	}
    static ajax(options){
		let headers = {'Content-Type':'application/x-www-form-urlencoded'};
		let url = options.url;
		let method = options.method;
		let data = "";
		if(options.method === 'get'){
			url = url + '?CSRF=' + getCsrfToken();
		}else{
			//有headers的情况是application/json格式提交
			if(options.headers){
				headers = options.headers;
				data = JSON.stringify({ ...options.data, CSRF:getCsrfToken() });
				url = options.url + '?CSRF=' + getCsrfToken();
			}else{
				data = Qs.stringify({ ...options.data, CSRF:getCsrfToken() });
			}
		}
        return new Promise((resolve,reject)=>{
            axios({
				headers,
                baseURL,
                url,
                method,
				data,
                timeout:5000,
				withCredentials: true,
				crossDomain: true
            }).then((response)=>{
                if (response.status === 200){
                    let res = response.data;
                    if (res.code === 0){
                        resolve(res);
                    }else if(res.code === 999 || res.code === 2001){
						const history = createHashHistory();
						history.push('/login');
					}else{
						let index = res.msg.indexOf(" call");
						let message = res.msg;
						if(index>0){
							message = message.slice(0, index);
						}
						if(url !== "/User/Login/login"){
							notification.open({
								message: '提示',
								description: message,
								icon: <ExclamationCircleOutlined style={{ color: '#108ee9' }} />
							});
						}
						reject(res);
                    }
                }else{
                    reject(response.data);
                }
            })
        });
    }
	static excelAjax(options){
		let url = baseURL + options.url + '?CSRF=' + getCsrfToken();
		window.location.href = url;
	}
	static upload(options){
		let headers = {'Content-Type':'multipart/form-data'};
		let url = options.url + "?CSRF=" + getCsrfToken();
		let method = "post";
		let data = options.data;
	    return new Promise((resolve,reject)=>{
	        axios({
				headers,
	            baseURL,
	            url,
	            method,
				data,
	            timeout:5000,
				withCredentials: true,
				crossDomain: true
	        }).then((response)=>{
	            if (response.status === 200){
	                let res = response.data;
	                if (res.code === 0){
	                    resolve(res);
	                }else if(res.code === 999 || res.code === 2001){
						const history = createHashHistory();
						history.push('/login');
					}else{
						let index = res.msg.indexOf(" call");
						let message = res.msg;
						if(index>0){
							message = message.slice(0, index);
						}
						notification.open({
							message: '提示',
							description: message,
							icon: <ExclamationCircleOutlined style={{ color: '#108ee9' }} />
						});
						reject(res);
	                }
	            }else{
	                reject(response.data);
	            }
	        })
	    });
	}
}